﻿package javase.io;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

class FileDemo01 extends JFrame implements ActionListener
{
	JPanel pnl_Main;
	JLabel lbl_Name;
	JLabel lbl_Pass;
	JTextField txt_Name;
	JPasswordField txt_Pass;
	JButton btn_Submit;
	JButton btn_Clear;
	String CrLf;
	public FileDemo01()
	{
		super("登录");
		CrLf=String.valueOf((char)(13))+String.valueOf((char)(10));
		pnl_Main=new JPanel();
		pnl_Main.setLayout(null);
		lbl_Name=new JLabel("用户名");
		lbl_Pass=new JLabel("密码");
		txt_Name=new JTextField();
		txt_Pass=new JPasswordField();
		btn_Submit=new JButton("提交");
		btn_Clear=new JButton("清空");
		btn_Submit.addActionListener(this);
		btn_Clear.addActionListener(this);
		AddComponent(lbl_Name,10,10,50,20);
		AddComponent(lbl_Pass,10,40,50,20);
		AddComponent(txt_Name,60,10,100,20);
		AddComponent(txt_Pass,60,40,100,20);
		AddComponent(btn_Submit,10,70,60,25);
		AddComponent(btn_Clear,100,70,60,25);
		this.setContentPane(pnl_Main);
		this.setLocation(300,300);
		this.setResizable(false);
		this.setSize(180,130);
		this.show();
	}
	public void AddComponent(JComponent jc,int left,int top,int width,int height)
	{
		jc.setLocation(left,top);
		jc.setSize(width,height);
		pnl_Main.add(jc);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==btn_Clear)
		{
			txt_Name.setText(null);
			txt_Pass.setText(null);
		}
		if(e.getSource()==btn_Submit)
		{
			try
			{
				String Info=txt_Name.getText()+":"+txt_Pass.getText();
				RandomAccessFile raf=new RandomAccessFile("record.txt","rw");
				raf.seek(raf.length());
				raf.writeBytes(Info+CrLf);
				raf.close();
				JOptionPane.showMessageDialog(this,"保存数据成功！","提示",JOptionPane.INFORMATION_MESSAGE);
			}
			catch(Exception ex)
			{
				JOptionPane.showMessageDialog(this,"保存数据失败！","提示",JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	public static void main(String []args)
	{
		new FileDemo02();
	}
}